package com.relax.netty.secondexample;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class MyClient {
    public static void main(String[] args) {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new MyClientInitializer());
            ChannelFuture channelFuture = bootstrap.connect("localhost", 8899).sync();
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if(channelFuture.isSuccess()){
                        System.out.println("连接成功....");
                    }else {
                        System.out.println("连接失败.....");
                    }
                }
            });
            System.out.println("111111111111111111111111");

            //关闭通道进行监听
            channelFuture.channel().closeFuture().sync();
            System.out.println("2222222222222222222");

        } catch (Exception e) {
            eventLoopGroup.shutdownGracefully();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}
